package jmine.tec.web.servlet.session;

import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

import jmine.tec.utils.debug.impl.DebugRuntimeFactory;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * {@link HttpSessionAttributeListener} que verifica todo objeto colocado na sessao
 * 
 * @author takeshi
 */
public class SerializableCheckerSessionAttributeListener implements HttpSessionAttributeListener {

    private static final Log LOGGER = LogFactory.getLog(SerializableCheckerSessionAttributeListener.class);

    /**
     * {@inheritDoc}
     */
    public void attributeAdded(HttpSessionBindingEvent se) {
        attributeReplaced(se);
    }

    /**
     * {@inheritDoc}
     */
    public void attributeRemoved(HttpSessionBindingEvent se) {
    }

    /**
     * {@inheritDoc}
     */
    public void attributeReplaced(HttpSessionBindingEvent se) {
        if (DebugRuntimeFactory.ENABLED) {
            if (!SessionCleanerActivationListener.isSerializable(se.getValue())) {
                RuntimeException error = new RuntimeException("session attribute is not Serializable!");
                if (DebugRuntimeFactory.getInstance().getPropertyAsBoolean("session.serialization.error")) {
                    throw error;
                } else {
                    LOGGER.error("Session attribute added: " + se.getName(), error);
                }
            }
        }
    }

}
